﻿using MediatR;
using SmartCity.ProcessEngine.API.Read.Application.Commands.FormTypes;
using SmartCity.ProcessEngine.API.Read.DTO;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Read.Application.CommandHandlers.FormTypes
{
    /// <summary>
    /// 表单分类查询命令处理
    /// </summary>
    public class FormTypeQueryCommandHandler : IRequestHandler<FormTypeQueryCommand, ApiResult<ApiPaging<FormTypeDto>>>
    {
        private readonly IBaseRepository<Industry> industryRep;
        private readonly IBaseRepository<WF_FromType> formTypeRep;

        public FormTypeQueryCommandHandler(IBaseRepository<Industry> industryRep, IBaseRepository<WF_FromType> formTypeRep)
        {
            this.industryRep = industryRep;
            this.formTypeRep = formTypeRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回分页集合任务</returns>
        public Task<ApiResult<ApiPaging<FormTypeDto>>> Handle(FormTypeQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<FormTypeDto>> result = new ApiResult<ApiPaging<FormTypeDto>>();
            result.code = ApiEnum.SearchSuccess;
            result.message = ApiEnum.SearchSuccess.ToString()+GetHashCode();

            try
            {
                var list = from a in formTypeRep.GetAll()
                           join b in industryRep.GetAll()
                           on a.IndustryId equals b.IndustryId
                           where !a.IsDeleted
                           select new FormTypeDto
                           {
                               TypeId = a.TypeId,
                               TypeNo = a.TypeNo,
                               TypeName = a.TypeName,
                               IndustryId = a.IndustryId,
                               IndustryName = b.IndustryName,
                               IsDeleted = a.IsDeleted,
                           };
                if (request.IndustryId > 0)
                {
                    list = list.Where(x => x.IndustryId.Equals(request.IndustryId));
                }
                if (!string.IsNullOrEmpty(request.TypeName))
                {
                    list = list.Where(x => x.TypeName.Contains(request.TypeName));
                }

                int ListCount = list.Count();
                int PageCount = (int)Math.Ceiling(ListCount * 1.0 / request.PageSize);
                list = list.OrderBy(x => x.TypeId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

                ApiPaging<FormTypeDto> paging = new ApiPaging<FormTypeDto>();
                paging.totalCount = ListCount;
                paging.totalPage = PageCount;
                paging.data = list.ToList();

                result.data = paging;
            }
            catch (Exception ex)
            {
                result.code = ApiEnum.SearchFail;
                result.message = ApiEnum.LoginFail.ToString() +ex.Message;
            }
            
            return Task.FromResult(result);
        }
    }
}
